package com.easy.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.easy.bean.Orders;
import com.easy.bean.User;
import com.easy.service.IOrdersService;
import com.easy.util.LayData;
import com.easy.util.Page;
import com.easy.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("orders")
public class OrdersController {
    @Autowired
    IOrdersService ordersService;

    @RequestMapping("list")
    @ResponseBody
    public LayData getOrdersList(Page page, String text){
        List<Orders> list=ordersService.selectOrders(page,text);
        int count=ordersService.OrdersCount(text);
        return new LayData(count,list);
    }

    @RequestMapping("delete")
    @ResponseBody
    public int deleteOrders(int id){
        return ordersService.deleteOrders(id);
    }

    @RequestMapping("add")
    @ResponseBody
    public Orders addOrders(HttpSession session,Orders orders){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        orders.setUserid(loginUser.getId());
        Snowflake snowflake = IdUtil.createSnowflake(1,1);
        long id = snowflake.nextId();
        orders.setCode(String.valueOf(id));
        ordersService.addOrders(orders);
        return orders;
    }

    @RequestMapping("edit")
    @ResponseBody
    public int editOrders(Orders orders){
        return ordersService.editOrders(orders);
    }

    @RequestMapping("get")
    @ResponseBody
    public List<Orders> getOrderdetailsByUserid(HttpSession session){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        int userid=loginUser.getId();
        return ordersService.getOrderdetailsByUserid(userid);
    }

    @RequestMapping("getOrder")
    @ResponseBody
    public Orders getOrderByOrderId(int orderid){
        return ordersService.getOrderByOrderId(orderid);
    }

//    @RequestMapping("createorder")
//    @ResponseBody
//    public String createOrder(){
//        return ordersService.createOrder();
//    }

    @RequestMapping("payment")
    @ResponseBody
    public String payment(int orderid){
        return ordersService.payment(orderid);
    }

    @RequestMapping("paysuccess")
    @ResponseBody
    public String paysuccess(@RequestParam HashMap map){
        System.out.println(map);
        String code=(String)map.get("out_trade_no");
        System.out.println(code);

        Orders order=ordersService.getOrderByCode(code);

        order.setStatus("已支付");

        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将当前时间格式化为字符串
        String formattedDateTime = now.format(formatter);

        order.setPaymenttime(formattedDateTime);

        ordersService.editOrders(order);

        return "success";
    }
}

















